package com.stylefeng.guns.core.util.excels.config.impl;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.stylefeng.guns.core.util.excels.config.ConfigConstant;
import com.stylefeng.guns.core.util.excels.config.ExcelConfigFactory;
import com.stylefeng.guns.core.util.excels.config.ExcelConfigManager;
import com.stylefeng.guns.core.util.excels.config.ReturnConfig;
import com.stylefeng.guns.core.util.excels.config.ReturnPropertyParam;
/**
 * @author horsery
 * @filename ExcelConfigManagerImpl.java
 * @createTime 2008-4-10 上午11:19:34
 * @version 
 * Excel配置管理
 */
public class ExcelConfigManagerImpl implements ExcelConfigManager {
	private final String configName="ImportExcelToModel.xml";
	private SAXReader saxReader;
	private Document doc;
	private Element root;
	
	public ExcelConfigManagerImpl(){
		InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(configName);
		saxReader=new SAXReader();
		try {
			doc=saxReader.read(in);
			//doc=saxReader.read(new File("E:\\eclipse\\workspace\\warehouse\\WebRoot\\WEB-INF\\conf\\"+this.configName));
			root=doc.getRootElement();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * 返回一个实体配置属性对像
	 * @param modelName XML实体ID名称
	  * @return RuturnConfig 
	 */
	public ReturnConfig getModelConfig(String modelName) {
		// TODO Auto-generated method stub
		Element model = this.getModelElement(modelName);
		ReturnConfig result = new ReturnConfig();
		if(model!=null){
			result.setClassName(model.attributeValue(ConfigConstant.MODEL_CLASS));
			result.setPropertyMap(this.getPropertyMap(model));			
		}
		return result;
	}
	

	/**
	 * 根据名称取得XML的一个元素
	 * @param modelName XML实体ID名称
	 * @return Element XML节点元素	
	 */
	public Element getModelElement(String modelName) {
		// TODO Auto-generated method stub
		List<Element> elLst=root.elements();
		Element model = null;
		Element returnModel =null;
		for(Iterator<Element> it=elLst.iterator();it.hasNext();){
			model=it.next();
			if(model.attributeValue("id").equals(modelName)){
				returnModel=model;
				break;
			}
		}
		return returnModel;
	}
	
	/**
	 * 得到每个属性的配参数
	 * @param Element XML节点元素
	 * @return Map
	 */
	private Map<String,ReturnPropertyParam> getPropertyMap(Element model){
		Map<String,ReturnPropertyParam> propertyMap = new HashMap<String,ReturnPropertyParam>();
		List<Element> elLst=model.elements();
		Element property=null;
		for(Iterator<Element> it=elLst.iterator();it.hasNext();){
			property=it.next();
			ReturnPropertyParam modelProperty = new ReturnPropertyParam();
			modelProperty.setName(property.attributeValue(ConfigConstant.PROPERTY_NAME));
			modelProperty.setColumn(property.attributeValue(ConfigConstant.PROPERTY_CLOUMN));
			modelProperty.setExcelTitleName(property.attributeValue(ConfigConstant.PROPERTY_EXCEL_TITLE_NAME));
			modelProperty.setDataType(property.attributeValue(ConfigConstant.PROPERTY_DATA_TYPE));
			modelProperty.setMaxLength(property.attributeValue(ConfigConstant.PROPERTY_MAX_LENGTH));			
			modelProperty.setFixity(property.attributeValue(ConfigConstant.PROPERTY_FIXITY));
			modelProperty.setCodeTableName(property.attributeValue(ConfigConstant.PROPERTY_CODE_TABLE_NAME));
			modelProperty.setDefaultValue(property.attributeValue(ConfigConstant.PROPERTY_DEFAULT));
			modelProperty.setDateFormat(property.attributeValue(ConfigConstant.DATA_FORMAT));
			modelProperty.setRequire(property.attributeValue(ConfigConstant.PROPERTY_REQUIRE));
			propertyMap.put(modelProperty.getExcelTitleName(), modelProperty);
			
		}
		return propertyMap;
	}
	
	public static void main(String args[]){
		ReturnConfig returnConfig = ExcelConfigFactory.createExcelConfigManger().getModelConfig("specialModelTemporary");
	}

}
